//
//  KLSGSelectedKindView.m
//  KaoLaShiGuangFM
//
//  Created by lanou3g on 15/11/5.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "KLSGSelectedKindView.h"

@interface KLSGSelectedKindView () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, copy) void(^action)(CGFloat);

@end

KLSGSelectedKindView *backgroundV;
UICollectionView *collectionView;

@implementation KLSGSelectedKindView

+ (void)addPellCollectionViewWithWindowFrame:(CGRect)frame selectData:(NSMutableArray *)titleArray action:(void(^)(CGFloat))action
{
    if (backgroundV) {
        [KLSGSelectedKindView hidden];
    }
    
    UIWindow *window = [[UIApplication sharedApplication].windows firstObject];
    backgroundV = [[KLSGSelectedKindView alloc] initWithFrame:frame];
    backgroundV.action = action;
    backgroundV.titleArray = titleArray;
    backgroundV.backgroundColor = [UIColor colorWithHue:0
                                                saturation:0
                                                brightness:0 alpha:0.4];
    [window addSubview:backgroundV];

    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, 50)];
    view.backgroundColor = [UIColor whiteColor];
    
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    imageView1.image = [UIImage imageNamed:@"ahh.jpg"];
    imageView1.userInteractionEnabled = YES;
    [view addSubview:imageView1];
    
    UIButton *backBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [backBtn setTitle:@"收起" forState:(UIControlStateNormal)];
    [backBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    backBtn.frame = CGRectMake(kScreenWidth - 70, 10, 60, 30);
    [backBtn  addTarget:self action:@selector(back:) forControlEvents:(UIControlEventTouchUpInside)];
    [view addSubview:backBtn];
    [backgroundV addSubview:view];
    
    
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 100, 30)];
    lable.text = @"请选择类别:";
    [view addSubview:lable];
    
    // 添加collectionview
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 5;
    layout.sectionInset = UIEdgeInsetsMake(20, 10, 20, 10);
    layout.itemSize = CGSizeMake((kScreenWidth - 70) / 3, 30);
    
    collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(20, 50, kScreenWidth - 40 , 0) collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.dataSource = backgroundV;
    collectionView.delegate = backgroundV;
    
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"item"];
    [window addSubview:collectionView];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 50, kScreenWidth - 40, ceilf(titleArray.count / 3.0) * 35 + 40)];
    imageView.image = [UIImage imageNamed:@"ah.jpg"];
    imageView.userInteractionEnabled = YES;
    [collectionView setBackgroundView:imageView];
    
    // 添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBackgroundClick)];
    [backgroundV addGestureRecognizer:tap];
    
    // 动画
    backgroundV.alpha = 0;
    collectionView.frame = CGRectMake(kScreenWidth, 0, 0, 0);
    
    [UIView animateWithDuration:0.3 animations:^{
        backgroundV.alpha = 1;
        collectionView.frame = CGRectMake(20, 150, kScreenWidth - 40 , ceilf(titleArray.count / 3.0) * 35 + 40) ;
        collectionView.layer.cornerRadius = 20;
    }];
}

+ (void)tapBackgroundClick
{
    [KLSGSelectedKindView hidden];
}

+ (void)hidden
{
    [backgroundV removeFromSuperview];
    [collectionView removeFromSuperview];
    collectionView = nil;
    backgroundV = nil;
}

+ (void)back:(UIButton *)sender
{
    [KLSGSelectedKindView hidden];
}

#pragma mark -实现collectionView的代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return  self.titleArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];
    // 移除cell上已添加的视图
    NSArray *subViews = cell.subviews;
    for (UIView *view in subViews) {
        [view removeFromSuperview];
    }
    NSString *title = self.titleArray[indexPath.row];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
//    label.backgroundColor = [UIColor grayColor];
    [cell addSubview:label];
    label.text = title;
    label.font = [UIFont systemFontOfSize:13];
    label.textAlignment = NSTextAlignmentCenter;
    label.layer.cornerRadius = 5;
    label.layer.masksToBounds = YES;
    return cell;
}

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    NSString *title = self.titleArray[indexPath.row];
//    CGSize size = [title sizeWithAttributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:17] forKey:NSFontAttributeName]];
//    return CGSizeMake(size.width, 30);
//}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.action) {
        self.action(indexPath.row);
    }
    [KLSGSelectedKindView hidden];
}


@end
